package window.component;

import javax.swing.*;
import java.awt.*;
import entity.FlightInfo;
import manage.FlightDBManager;
import manage.OrderDBManager;
import manage.UsernameController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FlightCard extends JPanel {
    public FlightCard(FlightInfo flightInfo) {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        setBackground(new Color(245, 245, 245)); // Light gray background for better contrast
        setPreferredSize(new Dimension(650, 180));

        // Flight Number Information
        JPanel flightNumberPanel = new JPanel();
        flightNumberPanel.setLayout(new BoxLayout(flightNumberPanel, BoxLayout.Y_AXIS));
        flightNumberPanel.setPreferredSize(new Dimension(100, 150));
        flightNumberPanel.setBackground(new Color(245, 245, 245));
        JLabel flightNumberLabel = new JLabel(flightInfo.getName(), SwingConstants.CENTER);
        flightNumberLabel.setFont(new Font("Arial", Font.BOLD, 16));
        flightNumberLabel.setForeground(new Color(0, 123, 255)); // Dark blue for flight number
        flightNumberLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        flightNumberPanel.add(Box.createVerticalStrut(20));
        flightNumberPanel.add(flightNumberLabel);

        // Flight Time and Price Information
        JPanel flightInfoPanel = new JPanel(new GridLayout(3, 3));
        flightInfoPanel.setBackground(new Color(245, 245, 245));
        flightInfoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        JLabel departureTimeLabel = new JLabel(flightInfo.getDepartureTime().format(timeFormatter), SwingConstants.CENTER);
        departureTimeLabel.setFont(new Font("Arial", Font.BOLD, 16));
        Duration duration=Duration.between(flightInfo.getDepartureTime(),flightInfo.getArriveTime());//Calculate the journey time
        long hours=duration.toHours();
        long minutes=duration.toMinutes() %60;
        String flighttime= hours +"h "+ minutes +"m";
        JLabel travelTimeLabel = new JLabel(flighttime, SwingConstants.CENTER); // Placeholder for travel time
        travelTimeLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        JLabel arrivalTimeLabel = new JLabel(flightInfo.getArriveTime().format(timeFormatter) , SwingConstants.CENTER);
        arrivalTimeLabel.setFont(new Font("Arial", Font.BOLD, 16));
        JLabel departureAirportLabel = new JLabel(flightInfo.getDeparture(), SwingConstants.CENTER);
        departureAirportLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        JLabel arrivalAirportLabel = new JLabel(flightInfo.getDestination(), SwingConstants.CENTER);
        arrivalAirportLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        JLabel nonstopLabel = new JLabel("Nonstop", SwingConstants.CENTER);
        nonstopLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        nonstopLabel.setForeground(new Color(34, 139, 34)); // Green to emphasize nonstop

        JLabel priceLabel = new JLabel("CNY " + flightInfo.getPrice(), SwingConstants.CENTER);
        priceLabel.setFont(new Font("Arial", Font.BOLD, 16));
        priceLabel.setForeground(Color.red); // Dark red to highlight price
        JButton selectButton = new JButton("Select");
        selectButton.setFont(new Font("Arial", Font.BOLD, 14));
        selectButton.setBackground(new Color(0, 123, 255)); // Blue button for emphasis
        selectButton.setForeground(Color.WHITE);
        selectButton.setFocusPainted(false);
        selectButton.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
        selectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                JFrame flightFrame = (JFrame) SwingUtilities.getWindowAncestor(selectButton);
                if(UsernameController.getUsername()==null){
                    JOptionPane.showMessageDialog(flightFrame, "Please login first!", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
                    flightFrame.dispose();
                }else {
                    FlightDBManager.updateFlightSeat(flightInfo,-1);
                    LocalDate today = LocalDate.now();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    String formattedDate = today.format(formatter);
                    OrderDBManager.addOrder(UsernameController.getUsername(), formattedDate, "FLIGHT", flightInfo.getName(), 1);
                    JOptionPane.showMessageDialog(flightFrame, "Booking successful!", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
                    flightFrame.dispose();
                }
            }
        });
        flightInfoPanel.add(departureTimeLabel);
        flightInfoPanel.add(nonstopLabel);
        flightInfoPanel.add(arrivalTimeLabel);
        flightInfoPanel.add(departureAirportLabel);
        flightInfoPanel.add(travelTimeLabel);
        flightInfoPanel.add(arrivalAirportLabel);
        flightInfoPanel.add(priceLabel);
        flightInfoPanel.add(new JLabel()); // Placeholder to maintain grid structure
        flightInfoPanel.add(selectButton);

        // Adding panels to main panel
        add(flightNumberPanel, BorderLayout.WEST);
        add(flightInfoPanel, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(450, 90));
        this.setMaximumSize(new Dimension(800, 90));
    }

//    public static void main(String[] args) {
//        FlightInfo flightInfo = new FlightInfo("HU1234", 1163.0, "HGH T3", "PEK T2",
//                                                java.time.LocalTime.of(21, 45), java.time.LocalTime.of(0, 10), 120);
//        JFrame frame = new JFrame("Flight Panel Example");
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setSize(750, 220);
//        frame.add(new FlightCard(flightInfo));
//        frame.setVisible(true);
//    }
}